#!/usr/bin/python
"""
Googolplex - a Python class to query on Google

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""

import urllib
import htmllib
import httplib

from sgmllib import SGMLParser

class Googolplex(SGMLParser):
	def __init__(self, verbose = 0):
		SGMLParser.__init__(self, verbose)
		self.init()

	def handle_data(self, data):
		if self.divcounter == 1 and self.tablesopen == 0 and self.resultsection == 1 and self.opendiv == 1:
			if data=='Category: ':
				self.category = 1
                #blocco aggiunto da me
		if self.divcounter == 1 and self.tablesopen == 0 and self.resultsection == 1 and self.opendiv == 1:
			if data=='Description:':
				self.description = 1
		if self.divcounter == 1 and self.tablesopen == 0 and self.resultsection == 1 and self.opendiv == 1:
			if self.dict.has_key('Description'):
				self.dict['Description'] = self.dict['Description'] + data 
			else:
				self.dict['Description'] = data
		if self.divcounter == 1 and self.tablesopen == 0 and self.openanchor == 1 and self.resultsection == 1 and self.opendiv == 1:
			if self.dict.has_key('title'):
				self.dict['title'] = self.dict['title'] + data 
			else:
				self.dict['title'] = data
			
		
		
	def do_p(self, attrs):
		self.resultsection = 0

	def start_form(self, attrs):
		self.openform = 1

	def end_form(self):
		self.openform = 0

	def start_div(self, attrs):
		self.divcounter = self.divcounter + 1
		self.opendiv = 1

	def end_div(self):
		self.opendiv = 0

	def start_a(self, attrs):
		self.resultsection = self.resultsection + 1
		self.openanchor = 1
		self.dict = {}

		# Testing with different strategies
		if self.divcounter == 1 and self.tablesopen == 0 and self.resultsection == 1 and self.opendiv == 1:
			for attrname, value in attrs:
				if attrname == 'href':
					self.dict['url'] = value
					self.counter = self.counter + 1
					if self.counter == 1:
						self.firsturl = value

                else:
			if self.category == 1 and self.resultsection >= 1:
				for attrname, value in attrs:
					if attrname == 'href':
						t = self.result.pop()
						t['category'] = value
						self.result.append(t)
						self.category = 0
			

	def end_a(self):
		if self.divcounter == 1 and self.tablesopen == 0 and self.resultsection == 1 and self.opendiv == 1:
			self.openanchor = 0
			self.result.append(self.dict)

        def start_description(self, attrs):
                self.description = self.description + 1
		self.description = self.description + 1
        def end_description(self, attrs):
                self.description = self.description - 1
		

	def start_table(self, attrs):
		self.tablescounter = self.tablescounter + 1
		self.tablesopen = self.tablesopen + 1

	def end_table(self):
		self.tablesopen = self.tablesopen - 1

	def inithtml(self):
		self.tablescounter = 0
		self.tablesopen = 0
		self.resultsection = 0
		self.divcounter = 0
		self.openanchor = 0
		self.opendiv = 0
		self.counter = 0
		self.category = 0
		self.openform = 0
		self.firsturl = ''
                self.description = 0
                self.a=''
                self.chiudi='</body></html>\r\n'
	def init(self):
		self.inithtml()

		self.proxy = ''
		self.prevfirsturl = ''
		self.lang = ''
		self.maxr = 10
		self.dict = {}
		self.result = []

	def setproxy(self, proxy):
		self.proxy = proxy

	def language(self, lang):
		self.lang = lang

	def maxresults(self, total):
		self.maxr = total

	def search(self, query):
                self.x=0
		self.result = []
		self.prevfirsturl = ''
		for c in range(0,self.maxr,10):
			self.inithtml()

			if self.proxy == '':
				h = httplib.HTTP('www.google.com')
				if self.lang <> '':
					h.putrequest('GET', '/search?'+urllib.urlencode({'q':query, 'lr':self.lang, 'start':c, 'filter':1}))
				else:
					h.putrequest('GET', '/search?'+urllib.urlencode({'q':query,'start':c, 'filter':1}))
			else:
				h = httplib.HTTP(self.proxy)
				if self.lang <> '':
					h.putrequest('GET', 'http://www.google.com/search?'+urllib.urlencode({'q':query, 'lr':self.lang, 'start':c, 'filter':1}))
				else:
					h.putrequest('GET', 'http://www.google.com/search?'+urllib.urlencode({'q':query,'start':c, 'filter':1}))
				
			h.putheader('Accept', 'text/html')
			h.endheaders()
			errcode, errmsg, headers = h.getreply()
			if errcode == 200:
				f = h.getfile()
				#aggiunta a scopo conoscitivo
				if self.x==0:
                                    f1=file(("C:\\mymod\\consolida\\"+query.upper()+" report.htm"),"w")
                                    li=f.readlines()
                                    no=(25,26,27,30,31)
                                    for el in li[0:50]:
                                            if li.index(el) not in no:
                                		f1.write(el)
                                    f.close()
                                    f1.close()
                                else:
                                    f1=file(("C:\\mymod\\consolida\\"+query.upper()+" report.htm"),"a")
                                    li=f.readlines()
                                    for el in li[32:50]:
                                                if el.find('Previous')==-1 and el.find('Next')==-1:
                                                    if el.find('input type=text')==-1:
                                                        f1.write(el)
                                    f.close()
                                    f1.close()
                                s=''
                                for el in li:
                                    s+=el
                                self.x+=1
				self.feed(s)
				

			if self.firsturl == self.prevfirsturl:
				for i in range(0, self.counter):
					self.result.pop()
				break
			else:	
				self.prevfirsturl = self.firsturl

			if len(self.result) == 0: break

		if len(self.result) > self.maxr:
			for i in range(0, len(self.result) - self.maxr):
				self.result.pop()
                f1=file(("C:\\mymod\\consolida\\"+query.upper()+" report.htm"),"a")
                f1.write(self.chiudi)
                f1.close()
		return self.result


# test()

def test():
	gs = Googolplex()

	# Canonical Query!
	print gs.search('google')

	# Other Queries
	print gs.search('python')
	print gs.search('zeek')
	print gs.search('free web hosting')

	gs.language('lang_es')
	print gs.search('argentina fmi')


def test2():
	
	gs = Googolplex()

	gs.maxresults(15)
#	gs.setproxy('localhost:8080')

#	res = gs.search('linux')

#	res = gs.search('linux paapa papanata dkdjkj')






#	print res

	print "Total:",len(res)

	for r in res:
		print r['title'], r['url']



# main()

def main():

	test()

# Run example when it's running like an script

if __name__ == '__main__':
	main()
